import 'package:base_flutter/base_flutter.dart';
import 'package:custom_pop_up_menu/custom_pop_up_menu.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_yyt/models/medical_elder_entity.dart';
import 'package:ls_work/config/global_configs.dart';
import 'package:ls_work/ui/food/food_order_record_page.dart';
import 'package:ls_work/ui/food/single_order_select_place_page.dart';
import 'package:ls_work/ui/select_old_page.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/food/select_order_type_view_model.dart';
import 'package:saas_order_meal/model/elder_info_data.dart';
import 'package:saas_order_meal/saas_order_meal.dart';
import 'package:saas_order_meal/ui/elder_month_food_page.dart';

import '../../net/response/food/order_model_list_data.dart';
import '../../util/request_url_utils.dart';
import '../select_building_floor_page.dart';
import '../select_employee_page.dart';
import '../select_temp_human_page.dart';
import 'old_order_food_detail_page.dart';

class SelectOrderTypePage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _SelectOrderTypeState();
}

class _SelectOrderTypeState
    extends BaseMvvmListState<SelectOrderTypeViewModel, SelectOrderTypePage> {
  CustomPopupMenuController _controller = CustomPopupMenuController();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text('点餐'),
        actions: [
          CustomPopupMenu(
            controller: _controller,
            child: IconButton(icon: Icon(Icons.menu), onPressed: null),
            arrowColor: Colors.white,
            menuBuilder: () => ClipRRect(
              borderRadius: 5.borderRadius,
              child: IntrinsicWidth(
                child: Column(
                  children: [
                    CommonText('点餐记录')
                        .addToContainer(alignment: Alignment.center, height: 40)
                        .onTap(() {
                      _controller.hideMenu();
                      Go().push(FoodOrderRecordPage());
                    }),
                    CommonText('今日配餐')
                        .addToContainer(alignment: Alignment.center, height: 40)
                        .onTap(() {
                      _controller.hideMenu();
                      Go().push(SelectBuildingFloorPage());
                    }),
                    // CommonText('点餐详情').addToContainer(alignment: Alignment.center,height: 40).onTap(() {
                    //   _controller.hideMenu();
                    //   Go().push(SelectOldPage((old) {
                    //     Go().push(OldOrderFoodDetailPage(old.id.toString(),oldInfo: old,));
                    //   }));
                    //
                  ],
                ),
              ).addToContainer(
                  color: Colors.white,
                  padding: EdgeInsets.symmetric(horizontal: 15, vertical: 12)),
            ),
            pressType: PressType.singleClick,
            verticalMargin: -20,
          )
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  Widget createItemWidget(int index) {
    var colors = [Colors.green, Colors.blueAccent, Colors.red];
    var item = viewModel.listItems[index];
    var color = colors[index % colors.length];
    return CommonText(item.name,
            textSize: 20, fontWeight: FontWeight.bold, textColor: Colors.white)
        .addToContainer(
            height: 80,
            alignment: Alignment.center,
            decoration:
                BoxDecoration(color: color, borderRadius: 10.borderRadius))
        .onTap(() {
      if (item.orderTypeName == "按周") {
        // if(BuildConfig.isDebug){
        _showBottomSelect(item);
        // }else{
        //   Go().push(SelectOldPage((old) {
        //     Go().push(OldOrderFoodDetailPage(old.id.toString(),oldInfo: old.changeToFood(),));
        //   }));
        // }
      } else {
        Go().push(SingleOrderSelectPlacePage(
            item.orderType.toString(), item.id.toString()));
      }
    });
  }

  void _showBottomSelect(OrderModelListData item) {
    // var typeList = [
    //   PickerDataWithIdModel(pickName: "住户点餐",pickId: "0"),
    //   PickerDataWithIdModel(pickName: "员工点餐",pickId: "1"),
    //   PickerDataWithIdModel(pickName: "临时人员点餐",pickId: "2"),
    // ];
    showBottomSelect(context, items: ["住户点餐", "员工点餐", "临时人员点餐"],
        onSelect: (value) {
      if (value == "住户点餐") {
        Go().push(SelectOldPage(
          (old) {
            // if (RequestUrlUtils.code == "xcyg") {
            //   Go().push(SingleOrderSelectPlacePage(
            //     item.orderType.toString(),
            //     item.id.toString(),
            //     oldId: old.id.toString(),
            //   ));
            // } else {
            // Go().push(OldOrderFoodDetailPage(old.id.toString(),oldInfo: old.changeToFood(),));
            if (GlobalConfigs.useNewOrder) {
              SaasOrderMeal.setMenuModel(GlobalConfigs.useWeekModel?MenuModel.BY_WEEK:MenuModel.BY_DAY);
              Go().push(ElderMonthFoodPage(
                old.id.toString(),
                oldInfo: ElderInfoData(
                    name: old.name,
                    sex: old.sexName,
                    age: old.age.toStringOrNull(),
                    bedNo: old.bedNo,
                    imageHead: old.headimageId),
              ));
            } else {
              Go().push(OldOrderFoodDetailPage(
                old.id.toString(),
                oldInfo: old.changeToFood(),
              ));
            }
            // }
          },
          isMeal: true,
        ));
      }
      if (value == "员工点餐") {
        Go().push(SelectEmployeePage(
          onSelectListener: (human) {
            if (GlobalConfigs.useNewOrder) {
              SaasOrderMeal.setMenuModel(GlobalConfigs.useWeekModel?MenuModel.BY_WEEK:MenuModel.BY_DAY);
              Go().push(ElderMonthFoodPage(
                human.id.toString(),
                oldInfo: ElderInfoData(
                    name: human.name,
                    sex: human.sex,
                    age: human.age.toStringOrNull(),
                    bedNo: human.bedNo,
                    imageHead: human.headImage),
              ));
            } else {
              Go().push(OldOrderFoodDetailPage(
                human.id.toString(),
                oldInfo: human,
              ));
            }
          },
        ));
      }

      if (value == "临时人员点餐") {
        Go().push(SelectTempHumanPage(
          onSelectListener: (human) {
            // Go().push(OldOrderFoodDetailPage(human.id.toString(),oldInfo: human,));
            if (GlobalConfigs.useNewOrder) {
              SaasOrderMeal.setMenuModel(GlobalConfigs.useWeekModel?MenuModel.BY_WEEK:MenuModel.BY_DAY);
              Go().push(ElderMonthFoodPage(
                human.id.toString(),
                oldInfo: ElderInfoData(
                    name: human.name,
                    sex: human.sex,
                    age: human.age.toStringOrNull(),
                    bedNo: human.bedNo,
                    imageHead: human.headImage),
              ));
            } else {
              Go().push(OldOrderFoodDetailPage(
                human.id.toString(),
                oldInfo: human,
              ));
            }
          },
        ));
      }
    });
  }

  @override
  Widget get separatorDivider => Container(
        height: 10,
      );

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.symmetric(vertical: 16, horizontal: 16);

  @override
  SelectOrderTypeViewModel createViewModel() {
    return SelectOrderTypeViewModel(context);
  }
}
